package com.hire.es.controller;


import com.yjc.eo.SearchResumeEO;
import com.yjc.eo.Stu;
import com.yjc.hire.grace.result.GraceJSONResult;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/9/8
 * </p>
 */

@RestController
@RequestMapping("es")
public class HelloController {

    @Autowired
    private ElasticsearchRestTemplate template;

    @GetMapping("createIndex")
    public GraceJSONResult createIndex() {
        template.indexOps(Stu.class).create();
        return GraceJSONResult.ok();
    }

    @GetMapping("deleteIndex")
    public GraceJSONResult deleteIndex() {
        template.indexOps(Stu.class).delete();
        return GraceJSONResult.ok();
    }

    @GetMapping("save")
    public GraceJSONResult save() {
        Stu stu = new Stu();
        stu.setId(1001L);
        stu.setName("hire");
        stu.setAge(18);
        stu.setMoney(99.8f);
        stu.setDescription("this is a test");
        IndexQuery iq = new IndexQueryBuilder()
                .withObject(stu).build();
        IndexCoordinates ic = IndexCoordinates.of("stu");
        template.index(iq,ic);
        return GraceJSONResult.ok();
    }

    @GetMapping("match_all")
    public GraceJSONResult matchAll(Integer page,Integer pageSize) {

        if (page < 1) page = 1;
        page--;
        Pageable pageable = PageRequest.of(page, pageSize);
        Query query = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchAllQuery())
                .withPageable(pageable)
                        .build();
        SearchHits<SearchResumeEO> search = template.search(query, SearchResumeEO.class);
        List<SearchHit<SearchResumeEO>> hits = search.getSearchHits();
        List<SearchResumeEO> resumeList = hits.stream().map(SearchHit::getContent).collect(Collectors.toList());
        return GraceJSONResult.ok(resumeList);
    }

    @GetMapping("term")
    public GraceJSONResult term(Integer page,Integer pageSize) {
        if (page < 1) page = 1;
        page--;
        Pageable pageable = PageRequest.of(page, pageSize);
        Query query = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.termQuery("sex",1))
                .withPageable(pageable)
                .build();
        SearchHits<SearchResumeEO> search = template.search(query, SearchResumeEO.class);
        List<SearchHit<SearchResumeEO>> hits = search.getSearchHits();
        List<SearchResumeEO> resumeList = hits.stream().map(SearchHit::getContent).collect(Collectors.toList());
        return GraceJSONResult.ok(resumeList);
    }

}
